require(['init'], function () {
    var $pagination = $('#js-pagination')
    var pagination
    function getClassList(query, cb) {
        $.ajax({
            url: ' /api/user/current',
            success: function (data) {
                doctorId = data.data.userid;
                $.ajax({
                    url: '/api/teacher/user/' + doctorId,
                    // url: '/api/classes',
                    success: function (data) {
                        teacheruid = data.data.teacheruid;
                        ajaxMethod({
                            url: '/api/teacher/classes/'+teacheruid,
                            data: query || {},
                            success: function (data) {
                                cb && cb(data)
                                console.log(data)
                                if(data.list.length==0) {
                                    $('#J_classlist').html('<div style="margin-top: 50px;text-align: center;">'+i18next.t('input.nullresults')+'</div>');
                                    $pagination.hide()
                                    return;
                                }
                                var $classlistTable = $('#J_classlist').html('')
                                var $thead = $('<thead>\
                                                  <tr class="enroll">\
                                                    <th>'+i18next.t('table.no')+'</th>\
                                                    <th>'+i18next.t('table.classname')+'</th>\
                                                    <th>'+i18next.t('table.students')+'</th>\
                                                    <th>'+i18next.t('table.thelatesttimeforclass')+'</th>\
                                                    <th>'+i18next.t('table.completethelessonnumbertotalclasshours')+'</th>\
                                                    <th>'+i18next.t('table.handle')+'</th>\
                                                  </tr>\
                                                </thead>').appendTo($classlistTable);
                                var $tbody = $('<tbody class="student_list"></tbody>').appendTo($classlistTable)
                                $.each(data.list, function (index, item) {
                                    var $classItem = $('<tr class="enroll"></tr>');
                                    $classItem.append('<td>' + (index + 1) + '</td>');
                                    $classItem.append('<td>' + item.classname + '</td>');
                                    $classItem.append('<td>' + item.studentcount + '</td>');
                                    $classItem.append('<td>' +item.last + '</td>');
                                    //  $classItem.append('<td>' + (new Date(item.begindate).Format('YYYY-MM-DD') || '--') + '</td>');
                                    $classItem.append('<td>' + item.finished + '/'+item.total+'</td>');
                                    var $handleTd = $('<td></td>').appendTo($classItem)
                                    var $showbtn = $('<a href="class_details.html?classuid='+ item.classuid +'&name='+item.classname+'" class="btn btn-primary btn-sm btn-inverse ml5"><span style="color:#398dee">'+i18next.t('table.details')+'</span></a>').appendTo($handleTd)
                                    $tbody.append($classItem)
                                    $tbody.find("tr").each(function () {
                                        //console.log(this)
                                        var tdhtml = $(this).children();
                                        $.each(tdhtml, function (i, t) {
                                            $(t).text() == 'null' ? $(t).text('--') : $(t).text()
                                            $(t).text() == '0000-00-00' ? $(t).text('--') : $(t).text()
                                            $(t).text() == 'NaNaN-aN-aN' ? $(t).text('--') : $(t).text()
                                            $(t).text() == '' ? $(t).text('--') : $(t).text()
                                        })
                                    });
                                });
                                $pagination.show()
                            }
                        })
                    }
                })
            }
        });

    }
    //搜索
    function loadSearchData(querystr) {
        if(querystr) {
            pagination.filter.classname = querystr;
            pagination.filter.studentname = querystr;
            pagination.searchData()
        } else {
            pagination.reloadData()
        }
    }

    $('#J_class_search').on('click', function () {
        var $input = $(this).prev();
        var querystr = $.trim($input.val());
        loadSearchData(querystr)
    });
    $('.searchInput').on('keyup', function (e) {
        if (e.keyCode == 13) {
            var querystr = $.trim($(this).val());
            loadSearchData(querystr)
        }
    });
    pagination = new Pagination($pagination, getClassList)

});
